#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
检查给定文件是否为 64 位 Windows PE 可执行文件。
用法: python check_pe64.py <exe_path>
输出: True 或 False（无其他内容，便于脚本解析）
"""

import sys
import os


def is_64bit_pe(filepath):
    if not os.path.isfile(filepath):
        return False
    try:
        with open(filepath, 'rb') as f:
            # Check DOS header "MZ"
            dos_header = f.read(2)
            if dos_header != b'MZ':
                return False

            # Read offset to PE header (at 0x3C)
            f.seek(0x3C)
            pe_offset_bytes = f.read(4)
            if len(pe_offset_bytes) != 4:
                return False
            pe_offset = int.from_bytes(pe_offset_bytes, 'little')

            # Go to PE signature
            f.seek(pe_offset)
            pe_signature = f.read(4)
            if pe_signature != b'PE\x00\x00':
                return False

            # Machine type is at PE + 4 (2 bytes)
            f.seek(pe_offset + 4)
            machine = int.from_bytes(f.read(2), 'little')
            # 0x8664 = IMAGE_FILE_MACHINE_AMD64
            return machine == 0x8664
    except Exception:
        return False


if __name__ == '__main__':
    if len(sys.argv) != 2:
        print("False")
        sys.exit(1)
    exe_path = sys.argv[1]
    result = is_64bit_pe(exe_path)
    print("True" if result else "False")